package leetcode;

import java.util.ArrayList;

/**
 * @program: datastructureandalogorithm
 * @description:
 * @author: hmx
 * @create: 2021-12-12 00:39
 **/
public class LeetCode5936 {

    public int maximumDetonation(int[][] bombs) {
        ArrayList<Integer>[] list = new ArrayList[bombs.length];
        for (int i = 0; i < list.length; i++) {
            list[i] = new ArrayList<>();
        }
        for (int i = 0; i < list.length; i++) {
            for (int j = 0; j < list.length; j++) {
                if (i != j && (long) (bombs[i][0] - bombs[j][0]) * (bombs[i][0] - bombs[j][0])
                        + (long) (bombs[i][1] - bombs[j][1]) * (bombs[i][1] - bombs[j][1]) <= (long) bombs[i][2]
                        * bombs[i][2]) {
                    list[i].add(j);
                }
            }
        }
        int max = 0;
        for (int i = 0; i < bombs.length; i++) {
            max = Math.max(max, maximumDetonation(i, new boolean[list.length], list));
        }
        return max;
    }

    private int maximumDetonation(int index, boolean[] visited, ArrayList<Integer>[] list) {
        if (visited[index]) {
            return 0;
        }
        int count = 1;
        visited[index] = true;
        for (int i : list[index]) {
            count += maximumDetonation(i, visited, list);
        }
        return count;
    }

}
